package com.apes.fico.finance.model.costcollect;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = WaterElectricityRent.CONST_TABLE_NAME)
public class WaterElectricityRent extends Model {
    public final static String CONST_TABLE_NAME = "gl_Water_Electricity_Rent";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "水电租", padding = 8)
    @Comment("编号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("部门")
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_company_id", referencedColumnName = "id", nullable = false)
    @Comment("所属公司")
    private PartyRole deptCompany;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("申报公司")
    private PartyRole company;

    @JSONField(format = DateUtil.SHOW_MONTH_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @NotNull
    @Column(name = "year_month")
    @Comment("年月")
    private Date yearMonth;

    public void setYearMonth(String yearMonth) throws ParseException {
        Date parse = new SimpleDateFormat("yyyy-MM").parse(yearMonth);
        this.yearMonth=parse;
    }

    public void setYearMonth(Date yearMonth) throws ParseException {
        this.yearMonth=yearMonth;
    }

    @States(name = "类型", values = {
            @State(id = "provision", name = "计提"),
            @State(id = "bluntAccrual", name = "冲计提"),
            @State(id = "actually", name = "实际发生"),
    })
    @Column(name = "type")
    @Comment("类型")//accrual(计提) real(实际) bluntProvision(冲计提) bluntActually(冲实际发生)
    private String type;

    @Column(name = "water_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("水费")
    private double waterAmt = 0;

    @Column(name = "electricity_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("电费")
    private double electricityAmt = 0;

    @Column(name = "rent_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("店租")
    private double rentAmt = 0;

    @Column(name = "other_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("其他")
    private double otherAmt = 0;

    @Column(name = "summary")
    @Comment("摘要")
    private String summary;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("冲红标记")
    private boolean valid = true; //有效

    @Transient
    private boolean selected;

    public boolean getSelected() {
        return this.selected;
    }


}
